package com.iot08.api.service.mail;

import org.springframework.core.env.Environment;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author cwj
 * @description
 * @date 2025/5/7 10:40
 **/
@Service
public class EmailService {

    @Resource
    private JavaMailSender javaMailSender;

    @Resource
    private Environment env;

    /**
     * @description 发送邮件
     * @author cwj
     * @param [toUserEmail, code]
     * @return void
     * @date @time 2025/5/7 11:37
     **/
    public void sendVerifyCode(String toUserEmail, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(Objects.requireNonNull(env.getProperty("spring.mail.username"))); // 发件人
        message.setTo(toUserEmail); // 收件人
        message.setSubject("【一路守护】您的验证码"); // 主题
        message.setText("您好，您的验证码是：" + code + "，有效期为5分钟，请尽快使用。"); // 正文内容
        javaMailSender.send(message); // 发送邮件
    }
}
